;(function() {
	'use strict';
	angular.module('services.analysisSysOM.cleanCount')
		.factory('handlingTimeFactory', handlingTimeFactory);
		// .service('HandlingTimeServices',HandlingTimeServices);

	function handlingTimeFactory(COREDATA,Restangular,CoreService,CtrlShowAndHide,cleanCountService,CoreLoading) {
		
		var time = new Date();
		var nowTime = new Date(time.getFullYear(),time.getMonth(),time.getDate()-1);
		return {
			pageBind: pageBind,
			getPageData: getPageData,
			getHandlingTimeView: getHandlingTimeView,
			search: search,
			
		};
		function pageBind(){
			return {
				provinces: COREDATA.PROVINCES, //省份
				time: nowTime,
				provinceCodes: ''
			};
		}
		function getPageData(pageBind){
			var sendData = {
				provinceCodes: [],
				provinceNames: [],
				time: new Date()
			};
			if (pageBind.provinceCodes) {
				sendData.provinceCodes.push(pageBind.provinceCodes.provinceCode);
				sendData.provinceNames.push(pageBind.provinceCodes.provinceName);
			}
			sendData.time = CoreService.dateFormat(pageBind.time, 'yyyy-MM-dd');
			return sendData;
		}
		function getHandlingTimeView(params,callback){
			Restangular.all('sas/analysisSysOM/cleanCount/queryHandlingTimeShow').post(params).then(callback);
		}
		function search(sendData){
			var flag = CtrlShowAndHide.flag;
			flag.state = !flag.state;
			var send = getPageData(sendData);
			if(!flag.state){
				CoreLoading.setState(true);
				flag.show = 'view';
				var timeTitle = cleanCountService.getTimeTitle(send,'first');
				getHandlingTimeView(send,function(result){
					if(result.state === '10000' && result.data.length >= 1){
						var myEcharts = echarts.init(document.getElementById('doc'));
						flag.items = result.data;
						if(send.provinceCodes.length === 0){
							send.title =  timeTitle + ' 清结算业务处理时长';
						}else {
							send.title =  timeTitle+ ' ' + send.provinceNames.toString() + '清结算业务处理时长';
						}
						flag.title = send.title;
						var option = cleanCountService.getEchartsInstance(result,send.title,'ccBusinessName','handlingTime','处理时长','bar');
						option.tooltip = {
							show:true,
							formatter: function(params){
								return params.name+'<br/>'+params.seriesName+': '+params.value+'(MS)';
							}
						};
						myEcharts.setOption(option);
						exportView(send,flag,myEcharts);
						flag.arrow = true;
					}else if(result.data.length === 0){
						flag.state = true;
						flag.arrow = false;
						CoreService.dialog('查询无数据');
					}else {
						flag.state = true;
						flag.arrow = false;
						CoreService.dialog('查询错误');
					}
					CoreLoading.setState(false);
				});
			}
		}
		function exportView(send,flag,myEcharts){
			send.base64s = [];
			var b = myEcharts.getDataURL('png');
			send.base64s.push(b.split(',')[1]);
			flag.outData = JSON.stringify(send);
		}
		
	}
	
})();